---
title: "New Screening Policies Page Impact"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
---

```{r, include=FALSE, echo=FALSE, message=FALSE, warning=FALSE}
#Income verification experimentation revenue impact estimate

library(tidyverse)
library(prophet)
library(lubridate)
library(dygraphs)
library(xts)
library(quantmod)
library(flexdashboard)

#ns = as_tibble(read.csv("Income_Users_2022_01_18.csv")) #read in historic data -- use this for income verification experiment impact analysis

ns = as_tibble(read.csv("Income_Users_2022_02_01.csv")) #read in historic data -- use this for page change impact analysis

ns$DATE = as.Date(ns$Week)

#baseline
c = ns %>% filter(Week >= "2021-02-15") %>% select(DATE,Total) %>% rename(ds = 1, y = 2)
##prophet model

##prophet model

c = ns %>% filter(Week >= "2021-02-15") %>% select(DATE,Total) %>% rename(ds = 1, y = 2)

c1 = c[1:44,]

m <- prophet(c1, seasonality.mode = 'additive',weekly.seasonality=TRUE)
future <- make_future_dataframe(m, periods = 6, freq = 'week')
forecast_c <- predict(m, future)

#create xts object for plotting
z = forecast_c %>% select(ds, yhat, yhat_lower, yhat_upper)
z$y = c$y
z1 = xts(x = z, order.by = z$ds)

c$y[50] - forecast_c$yhat[41] #41 lost policies

c$y[50] - forecast_c$yhat_upper[50] #72 lost policies

c$y[50] - forecast_c$yhat_lower[50] #9 lost policies

```
```{r}
dygraph(z1) %>% dySeries(c("yhat_lower", "yhat", "yhat_upper"), label = "forecast") %>% dySeries("y", label = "actual") %>% dyOptions(colors = RColorBrewer::brewer.pal(3, "Set1")) %>% dyLegend(show = "onmouseover") %>% dyRangeSelector() %>% dyEvent("2021-12-15", "page change", labelLoc = "bottom")
```